import {
  addArticleCommentApi,
  addArticleLikeApi,
  addArticleViewApi,
  getArticleCommentApi,
  getArticleListApi,
  getLiftRecordApi
} from '@/api'
import type { ArticleCommentType, ArticleCommentListType } from '@/api'
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

// 文章文章列表
export const fetchArticleListAction = createAsyncThunk(
  'share/fetchArticleListAction',
  async (page: number, { dispatch }) => {
    const res: any = await getArticleListApi(page)
    return res
  }
)

// 发布文章评论
export const fetchAddArticleCommentAction = createAsyncThunk(
  'share/fetchAddArticleCommentAction',
  async (data: ArticleCommentType) => {
    const res = await addArticleCommentApi(data)
    return res
  }
)

// 根据文章id获得评论
export const fetchArticleCommentAction = createAsyncThunk(
  'share/fetchArticleCommentAction',
  async (data: ArticleCommentListType) => {
    const res = getArticleCommentApi(data.article_id, data.page, data.size)
    return res
  }
)

// 获得随记列表
export const fetchLiftRecordAction = createAsyncThunk(
  'share/fetchLiftRecordAction',
  async (page: number, { dispatch }) => {
    const res = await getLiftRecordApi(page)
    return res
  }
)

// 给文章点赞
export const fetchArticleLikeAction = createAsyncThunk(
  'share/fetchArticleLikeAction',
  async (id: number | string) => {
    const res = await addArticleLikeApi(id)
    console.log(res)
    return res
  }
)

// 增加文章预览量
export const fetchArticleViewAction = createAsyncThunk(
  'share/fetchArticleViewAction',
  async (id: number | string) => {
    const res = await addArticleViewApi(id)
    return res
  }
)

interface IState {
  articleList: any[] // 文章列表
}

const initialState: IState = {
  articleList: []
}

const shareReducer = createSlice({
  name: 'home',
  initialState: initialState,
  reducers: {}
})

export default shareReducer.reducer
